
package com.mrd.gtimpframe.service.impl;

import com.mrd.base.Jqgrid;
import com.mrd.gtimp.business.repository.RegisteredCompanyDao;
import com.mrd.gtimpframe.entity.AgentRef;
import com.mrd.gtimpframe.entity.AuditInfo;
import com.mrd.gtimpframe.repository.AgentRefMapper;
import com.mrd.gtimpframe.repository.AuditInfoMapper;
import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimp.business.entity.RegisteredCompany;
import com.mrd.gtimp.registered.ApproveRegAction;
import com.mrd.gtimpframe.repository.ComAuditMapper;
import com.mrd.gtimpframe.service.ComAuditService;
import com.mrd.gtimpframe.vo.ComInfoAuditVO;
import com.mrd.util.LoginUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Date;

import javax.annotation.Resource;
import javax.transaction.Transactional;

@Service
public class ComAuditServiceImpl implements ComAuditService {
    @Resource
    private ComAuditMapper comAuditMapper;
    @Autowired
    private ApproveRegAction approveRegAction;
    @Resource
    private AuditInfoMapper auditInfoMapper;
    @Resource
    private AgentRefMapper agentRefMapper;
    @Resource
    private RegisteredCompanyDao registeredCompanyDao;

    @Override
    public Page<ComInfoAuditVO> auditGridData(Jqgrid jqgrid) {
        jqgrid.pageWithMybatis();
        jqgrid.sortWithMybatis();
        return jqgrid.pageResult(comAuditMapper.auditGridData(jqgrid.getFilterMap()));
    }

    @Override
    public ComInfoAuditVO getAuditData(String registerType, String auditId) {
        if ("直接注册".equals(registerType)) {
            //查旧的公司注册表
            return comAuditMapper.getWaitingAuditDataRegesteredCom(auditId);
        } else {
            return comAuditMapper.getWaitingAuditDataRecessiveCom(auditId);
        }
    }

    /**
     * 查询公司审核信息
     */
    @Override
    public AuditInfo getAuditInfo(String agentComId, String recessiveComId) {
        return auditInfoMapper.selectByAgentAndRecId(agentComId, recessiveComId);
    }

    @Override
    public String getAgentRefIdByAuditId(String auditId) {
        return auditInfoMapper.selectByPrimaryKey(auditId).getRelationshipId();
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(Object id) {
        return auditInfoMapper.deleteByPrimaryKey((String) id);
    }

    @Override
    @Transactional
    public int insert(Object o) {
        return auditInfoMapper.insert((AuditInfo) o);
    }

    @Override
    @Transactional
    public int insertSelective(Object o) {
        return auditInfoMapper.insertSelective((AuditInfo) o);
    }

    @Override
    public Object selectByPrimaryKey(Object id) {
        return auditInfoMapper.selectByPrimaryKey((String) id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(Object o) {
        return auditInfoMapper.updateByPrimaryKeySelective((AuditInfo) o);
    }

    @Override
    @Transactional
    public int updateByPrimaryKey(Object o) {
        return auditInfoMapper.updateByPrimaryKey((AuditInfo) o);
    }

    @Override
    @Transactional
    public Result didComAudit(String registerType, String auditId, boolean pass, String remark) {
        if("直接注册".equals(registerType)){
            return auditSystemCompanyRegister(auditId, pass, remark);
        }else{
            //1 检查审核记录
            AuditInfo auditInfo = auditInfoMapper.selectByPrimaryKey(auditId);
            if(auditInfo == null){
                return Result.newFailInstance("操作失败，无效的注册记录");
            }
            if("1".equals(auditInfo.getDelFlag())){
                return Result.newFailInstance("操作失败，无效的注册记录");
            }
            if("2".equals(auditInfo.getStatus())){
                return Result.newFailInstance("操作失败，注册记录已完成【审核不通过】操作");
            }
            if("1".equals(auditInfo.getStatus())){
                return Result.newSuccessInstance("操作完成");
            }
            //2 检查委托关系
            AgentRef agentRef = agentRefMapper.selectByPrimaryKey(auditInfo.getRelationshipId());
            if(agentRef == null){
                return Result.newFailInstance("操作失败，丢失委托关系");
            }
            if("1".equals(agentRef.getDelFlag())){
                return Result.newFailInstance("操作失败，丢失委托关系");
            }
            if("1".equals(agentRef.getStatus())){
                return Result.newSuccessInstance("操作完成");
            }

            //更新审核状态、备注
            auditInfo.setStatus(pass?"1":"2");
            auditInfo.setRemark(remark);
            auditInfo.setAuditor(LoginUtil.getLoginUserId());
            auditInfo.setAuditTime(new Date());
            auditInfoMapper.updateByPrimaryKeySelective(auditInfo);
            //更新委托关系为生效
            agentRef.setStatus("1");
            agentRefMapper.updateByPrimaryKeySelective(agentRef);
        }
        return Result.newSuccessInstance("操作完成");
    }

    /**
     * 公司注册审核
     * @param comRegisterId 公司注册ID
     * @param pass 审核结果
     * @param remark 审核备注
     */
    @Transactional
    public Result auditSystemCompanyRegister(String comRegisterId, boolean pass, String remark){
        RegisteredCompany registeredCompany = registeredCompanyDao.findOne(comRegisterId);
        registeredCompany.setPlatform_approve_result(remark);
        registeredCompany.setPlatform_approve_status(pass? "0" : "1");
        return approveRegAction.saveOrUpdate(registeredCompany);
    }
}
